<template>
	<view style="padding: 30rpx;">
		<button @click="onSetTimeCall()">单词的定时器setTimeout</button>
		<button @click="onSetTimeCallxy(userName,password)">单词的定时器setTimeCallxy</button>
		<button @click="onTimeoutClear()" >取消定时器setTimeCallxy</button>
		<button @click="onInteral()" >周期打印</button>
		<button @click="onClearInter()" >取消周期打印</button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				userName:'lvqiang',
				password:'666',
				timeoutID:null,
				count:0, 
				interalID:null
			}
		},
		methods: {
			onSetTimeCall(){
				console.log("onSetTimeCall-->");
				// setTimeout(callback,ms,any[])
				this.timeoutID=setTimeout(()=>{
					// 延时之后要执行的代码2000为毫秒
					console.log("我延时2秒才会打印");
				},2000)
			},
			onSetTimeCallxy(name,pwd){
				console.log("onSetTimeCallxy--> name"+name+",pwd:"+pwd);
				// console.log("onSetTimeCallxy--> name"+this.userName+",pwd:"+this.password);
				setTimeout((x,y)=>{
					console.log("我可以传参数了>x:"+x+",y:"+y);
				},2000,name,pwd)
			},
			onTimeoutClear() {
							if (this.timeoutID) {
								clearTimeout(this.timeoutID);
								console.log("定时器已取消：" + this.timeoutID);
								this.statusText = '定时器已取消'
								this.timeoutID = null // 取消后清空ID
							} else {
								console.log("没有可取消的定时器");
								this.statusText = '没有可取消的定时器'
							}
						},
			onInteral(){
				this.interalID=setInterval(()=>{
					this.count++;
					console.log("周期打印count:",this.count);
				},1000)
			},
			onClearInter() {
							if (this.interalID) {
								clearInterval(this.interalID)
								console.log("周期打印已取消", this.interalID);
								this.statusText = `周期打印已取消，最终count=${this.count}`
								this.interalID = null // 取消后清空ID
							} else {
								console.log("没有可取消的周期打印");
								this.statusText = '没有可取消的周期打印'
							}
						}
					
			
			
			
		}
	}
</script>

<style>

</style>
